#ifndef XVIDEOVIEW_H
#define XVIDEOVIEW_H
#include <mutex>
#include <fstream>

void MSleep(unsigned int ms);
long long NOwMs();
struct AVFrame;

class XVideoView
{
public:
    XVideoView();
    enum Format
    {
        YUV420P = 0,
        ARGB = 25,
        RGBA = 26,
        BGRA = 28
    };
    enum RenderType
    {
        SDL = 0,
    };

    static XVideoView* Create(RenderType type = SDL);

    bool DrawFrame(AVFrame* frame);
    //初始化渲染窗口
    virtual bool InIt(int w, int h,
                      Format FMT = RGBA) = 0;
    //渲染图像
    virtual bool Draw(const unsigned char * data, int linesize = 0) = 0;
    virtual bool Draw(const unsigned char* y, int y_pitch,
                    const unsigned char* u, int u_pitch,
                    const unsigned char* v, int v_pitch) = 0;

    virtual bool IsExit()=0;
    virtual void Close() = 0;

    void mirrorYUV420_Horizontal(unsigned char* yuv_data, int width, int height);

    void Scale(int w, int h)
    {
        scale_w_ = w;
        scale_h_ = h;
    }

    bool Open(std::string filepath);
    //读取一帧数据并且维护AVframe空间，每次调用会覆盖上一帧数据
    AVFrame *Read();
    void set_win_id(void* win) {wind_id_ = win;}
    int Getrender_fps() {return render_fps_;}
protected:
    int width_ = 0;
    int height_ = 0;
    Format fmt_ = RGBA;
    std::mutex mtx_;
    int scale_w_ = 0;
    int scale_h_ = 0;

    void* wind_id_ = nullptr;
    int render_fps_ = 0;
    int count_ = 0;
    long long beg_ms_ = 0;
private :
    std:: ifstream ifs_;
    AVFrame* frame_ = nullptr;
};

#endif // XVIDEOVIEW_H
